package com.example.content.repository

import com.example.content.repository.bean.*
import com.example.content.repository.files.FileWrapper
import com.example.core.network.HttpClient
import com.example.core.network.MoshiHelper
import com.example.core.network.body.UploadProgressRequestBody
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody


/**
 * Created by zhudongao
 * Desc
 * Date 2020/10/25
 */
object RepositoryMgr {

    private val service = HttpClient.apiService(ApiService::class.java)

    suspend fun login(userName: String, password: String): UserToken? {
        val loginReq = LoginReq(userName, password, "江苏南京")
        val body: RequestBody = jsonBody(MoshiHelper.toJson(loginReq))
        return service.login(body).apiData()
    }

    /**
     * 关注/取消关注
     */
    suspend fun follow(followReq: FollowReq): ApiResult<FollowResp> {
        return service.follow(jsonBody(MoshiHelper.toJson(followReq)))
    }

    /**
     * 获取用户的打赏记录
     */
    suspend fun getUserRewardInfo(id: Int, offset: Int) : ApiResult<List<UserRewardResp>>{
        val recodeInfo = UserRewardRecordReq(offset = offset, user_id = id)
        return service.getUserRewardInfo(jsonBody(MoshiHelper.toJson(recodeInfo)))
    }

    /**
     * 获取关注/粉丝 用户列表
     */
    suspend fun getFollowers(followersInfoReq: FollowersInfoReq): List<UserInfo>? {
        return service.getFollowers(jsonBody(MoshiHelper.toJson(followersInfoReq))).apiData()
    }

    suspend fun modifyUserInfo(userInfo: ModifyUserInfoReq): ApiResult<MdUserInfo> {
        return service.modifyUserInfo(jsonBody(MoshiHelper.toJson(userInfo)))
    }

    suspend fun getUserInfo(userInfoReq: UserInfoReq): ApiResult<List<UserInfo>> {
        return service.getUserInfo(jsonBody(MoshiHelper.toJson(userInfoReq)))
    }

    suspend fun getProductInfoById(idList: List<Int>): ApiResult<List<ProductInfo>> {
        val infoReq = QueryProductInfoReq(id_list = idList)
        return service.getProductInfoById(jsonBody(MoshiHelper.toJson(infoReq)))
    }

    //获取用户作品列表
    suspend fun getUserProducts(id: Int, type:Int): ApiResult<List<ProductInfo>> {
        val userProductReq = UserProductReq(user_id = id, type = type)
        return service.getUserProducts(jsonBody(MoshiHelper.toJson(userProductReq)))
    }

    //获取用户的收藏
    suspend fun getUserCollect(id :Int, offset:Int): ApiResult<List<ProductInfo>> {
        val userProduct = CollectReq(offset = offset, user_id = id)
        return service.getUserCollect(jsonBody(MoshiHelper.toJson(userProduct)))
    }

    suspend fun delProductById(id: Int): ApiResult<String> {
        return service.delProduct(id)
    }

    /**
     * 点赞, 收藏
     */
    suspend fun userAction(userActionReq: UserActionReq):ApiResult<String> {
        return service.userAction(jsonBody(MoshiHelper.toJson(userActionReq)))
    }

    suspend fun viewProductById(createId: Int) {
        service.viewProductById(createId)
    }

    /**
     * 获取作品的查看、收藏、点赞的用户
     */
    suspend fun getProductActionUser(creationId: Int, action: Int): ApiResult<List<UserInfo>> {
        val actionReq = ActionUserReq(creation_id = creationId, action = action)
        return service.getProductActionUser(jsonBody(MoshiHelper.toJson(actionReq)))
    }

    /**
     * 打赏作品
     */
    suspend fun rewardProduct(createId:Int, coin:Int) {
        val reward = RewardReq(createId, coin)
        service.rewardProduct(jsonBody(MoshiHelper.toJson(reward)))
    }

    /**
     * 获得作品的打赏记录
     */
    suspend fun getRewardRecordInfo(creationId: Int): ApiResult<List<RewardRecord>> {
        val recodeInfo = RewardRecordReq(creation_id = creationId)
        return service.getRewardRecordInfo(jsonBody(MoshiHelper.toJson(recodeInfo)))
    }

    //文件操作
    suspend fun uploadFile(fileWraps: List<FileWrapper>): List<String>? {
        val parts = mutableListOf<MultipartBody.Part>()
        fileWraps.forEach {
            val body = it.file.asRequestBody(it.contentType)
            if (it.responseCallback != null) {
                val uploadBody = UploadProgressRequestBody(body, it.responseCallback)
                parts.add(MultipartBody.Part.createFormData("files", it.fileName, uploadBody))
            } else {
                parts.add(MultipartBody.Part.createFormData("files", it.fileName, body))
            }
        }
        return service.uploadFile(parts).apiData()
    }

    //创建上传作品
    suspend fun uploadProduct(reqInfo:CreateProductReq): ApiResult<ProductInfo> {
        return service.createProduct(jsonBody(MoshiHelper.toJson(reqInfo)))
    }

//    suspend fun uploadPhotos(fileWraps: List<FileWrapper>) {
//        val build = MultipartBody.Builder()
//        fileWraps.forEach {
//            val requestBody = it.file.asRequestBody(it.contentType)
//            if (it.responseCallback != null) {
//                val uploadBody = UploadProgressRequestBody(requestBody, it.responseCallback)
//                build.addFormDataPart("files", it.fileName, uploadBody)
//            } else {
//                build.addFormDataPart("files", it.fileName, requestBody)
//            }
//            build.setType(MultipartBody.FORM)
//            service.uploadPhotos(build.build())
//        }
//    }

    //设置部分接口
    suspend fun feedBack(feedback: FeedbackReq): Int {
        val data = service.feedback(jsonBody(MoshiHelper.toJson(feedback)))
        return data.code
    }

    //搜索部分接口
    suspend fun searchProduct(searchReq: SearchReq): ApiResult<List<ProductInfo>> {
        return service.searchProduct(jsonBody(MoshiHelper.toJson(searchReq)))
    }

    suspend fun searchUser(searchReq: SearchReq): ApiResult<List<UserInfo>> {
        return service.searchUser(jsonBody(MoshiHelper.toJson(searchReq)))
    }

    suspend fun getSearchKey(key: String): ApiResult<List<String>> {
        return service.getSearchKey(key)
    }

    /**
     * 审核作品
     */
    suspend fun reviewProduct(reviewProduct: ReviewProduct) {
        service.reviewProduct(jsonBody(MoshiHelper.toJson(reviewProduct)))
    }

    /**
     * 获取推荐
     */
    suspend fun getRecommend(recommend:RecommendReq): ApiResult<List<ProductInfo>> {
        return service.getRecommend(jsonBody(MoshiHelper.toJson(recommend)))
    }


    private fun jsonBody(json: String): RequestBody {
        return json.toRequestBody("application/json".toMediaType())
    }
}